package com.xtc.netty.io.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Administrator
 */
public class BioServer {

    private ServerSocket server;

    public BioServer(int prot) {
        try {
            server = new ServerSocket(prot);

            System.out.println("服务端已启动，监听端口：" + prot);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void listen() throws IOException {
        while (true) {
            Socket client = server.accept();
            System.out.println(client.getPort());

            InputStream inputStream = client.getInputStream();

            byte[] buffer = new byte[1024];
            int len = inputStream.read(buffer);
            if (len > 0) {
                String msg = new String(buffer, 0, len);
                System.out.println("收到：" + msg);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new BioServer(9090).listen();
    }
}
